{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "api_secret = \"Vr0PtRCw-ZFwYXTKVfi6aDNaSqlfunK3\"\n",
    "api_key = 'jRpCaZ34kqNYJ8Zppdc-yGGum_YkETov'  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url = \"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603541727115&di=1fce44655650561f2347143eb796724f&imgtype=0&src=http%3A%2F%2Fimgwww.heiguang.net%2Fuploadfile%2F2016%2F1101%2F20161101052227509.jpg\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r = requests.post(BASE_URL, params=payload, headers=headers)\n",
    "r.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1603531849,3b2520e3-6e39-4197-a395-58c9c4f1395f\",\"time_used\":727,\"faces\":[{\"face_token\":\"922312bc0f660196fe6628faf9e1691b\",\"face_rectangle\":{\"top\":516,\"left\":773,\"width\":393,\"height\":393},\"attributes\":{\"gender\":{\"value\":\"Male\"},\"age\":{\"value\":81},\"smile\":{\"value\":100.000,\"threshold\":50.000},\"emotion\":{\"anger\":0.055,\"disgust\":0.073,\"fear\":0.256,\"happiness\":99.036,\"neutral\":0.013,\"sadness\":0.555,\"surprise\":0.013}}}],\"image_id\":\"TkRrOalHcLenPuYB19lhHQ==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603531849,3b2520e3-6e39-4197-a395-58c9c4f1395f',\n",
       " 'time_used': 727,\n",
       " 'faces': [{'face_token': '922312bc0f660196fe6628faf9e1691b',\n",
       "   'face_rectangle': {'top': 516, 'left': 773, 'width': 393, 'height': 393},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 81},\n",
       "    'smile': {'value': 100.0, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.055,\n",
       "     'disgust': 0.073,\n",
       "     'fear': 0.256,\n",
       "     'happiness': 99.036,\n",
       "     'neutral': 0.013,\n",
       "     'sadness': 0.555,\n",
       "     'surprise': 0.013}}}],\n",
       " 'image_id': 'TkRrOalHcLenPuYB19lhHQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# json转义\n",
    "results = r.json() \n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>request_id</th>\n",
       "      <th>time_used</th>\n",
       "      <th>faces</th>\n",
       "      <th>image_id</th>\n",
       "      <th>face_num</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1603531849,3b2520e3-6e39-4197-a395-58c9c4f1395f</td>\n",
       "      <td>727</td>\n",
       "      <td>[{'face_token': '922312bc0f660196fe6628faf9e16...</td>\n",
       "      <td>TkRrOalHcLenPuYB19lhHQ==</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                        request_id  time_used  \\\n",
       "0  1603531849,3b2520e3-6e39-4197-a395-58c9c4f1395f        727   \n",
       "\n",
       "                                               faces  \\\n",
       "0  [{'face_token': '922312bc0f660196fe6628faf9e16...   \n",
       "\n",
       "                   image_id  face_num  \n",
       "0  TkRrOalHcLenPuYB19lhHQ==         1  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# pandas数据表格化\n",
    "import pandas as pd\n",
    "df_face = pd.json_normalize(results)\n",
    "df_face"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
